<template>
    <div id="tmpl">
        <!--评论组件-->
        <!--1.0 实现提交评论数据到服务器的静态结构-->
        <div id="postcomment">
            <h3>提交评论</h3>
            <p class="p"></p>
            <textarea placeholder="请输入您要评论的内容..." v-model="postcontent"></textarea>
            <mt-button type="primary" size="large" @click="postcomment">发表</mt-button>
        </div>

        <!--2.0 实现获取评论数据列表-->
        <div id="list">
            <h3>评论列表</h3>
            <p class="p"></p>
            <div v-for="(item,index) in list">
                <div class="title">
                    <span>第{{index + 1}}楼:</span>
                    <span>用户：{{item.user_name}}</span>
                    <span>发表时间：{{item.add_time | datefmt('YYYY-MM-DD HH:mm:ss')}}</span>
                </div>
                <ul class="mui-table-view">
                    <li class="mui-table-view-cell" v-text="item.content"></li>
                </ul>
            </div>
        </div>

        <!--3.0 实现获取更多按钮-->
        <mt-button type="danger" size="large" plain @click="getmore">加载更多</mt-button>
    </div>
</template>
<script>
import common from '../../kids/common.js';
import { Toast } from 'mint-ui';
export default {
    data() {
        return {
            pageindex: 1,
            postcontent: '',
            list: []
        }
    },
    props: ['id'],
    created() {
        this.getcommentlist(this.pageindex);
    },
    methods: {
        //3.0 获取更多评论
        getmore(){
            //1.0实现this.pageindex值得增加
            this.pageindex++;
            //2.0获取当前this.pageindex对应的分页数据
            this.getcommentlist(this.pageindex);
        },
        //2.0获取评论列表
        getcommentlist(pageindex) {
            pageindex = pageindex || 1;
            //2.1.0确定获取地址
            var url = common.apidomain + '/api/getcomments/' + this.id + '?pageindex=' + pageindex;
            //2.2.0发送ajax 请求
            this.$http.get(url).then(function(res) {
                console.log(res.body.message);
                if (res.body.status != 0) {
                    Toast(res.body.message);
                    return;
                }
                //2.3.0将数据赋值给list
                //2.3.1将更新的数据追加到原数据的后面
                this.list = this.list.concat(res.body.message);

            })
        },
        //1.0提交评论信息
        postcomment() {
            //0.0 判断提交信息不能为空
            if (this.postcontent.trim().length <= 0) {
                Toast('您的评论不能为空');
                return;
            }
            //1.0 确定提交的地址
            var url = common.apidomain + '/api/postcomment/' + this.id;
            //2.0利用ajax的post请求提交信息
            this.$http.post(url, { content: this.postcontent }, { emulateJSON: true }).then(function(res) {
                console.log(res);
                var body = res.body;

                Toast(body.message);
                //提交后清空输入框
                this.postcontent = '';




            })
        }
    }
}
</script>
<style scoped>
/* 1.0 实现提交评论样式 */

#postcomment {
    padding: 5px;
}

.p {
    height: 1px;
    width: 100%;
    border-bottom: 1px solid rgba(0, 0, 0, 0.3);
}
</style>
